package com.xjm.api.admin;

import com.xjm.entity.vo.MenuVo;
import com.xjm.entity.vo.Result;
import com.xjm.constant.Update;
import com.xjm.entity.dto.MenuDto;
import com.xjm.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    //查询所有菜单
    @GetMapping("/list")
    public Result<List<MenuVo>> list() {
        return Result.success(menuService.getList());
    }


    @PostMapping("/addMenu")
    public Result<String> addMenu(@Validated @RequestBody MenuDto MenuDto) {
        MenuDto.setPath(MenuDto.getPath() == null ? "" : MenuDto.getPath());
        MenuDto.setStatus(1);
        if (menuService.addMenu(MenuDto) == 0) {
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }

    @GetMapping("/getMenuById/{id}")
    public Result<MenuVo> getMenuById(@PathVariable String id) {
        return Result.success(menuService.getMenuById(id));
    }

    @PostMapping("/updateMenu")
    public Result<Void> updateMenu(@Validated(Update.class) @RequestBody MenuDto MenuDto) {
        MenuDto.setPath(MenuDto.getPath() == null ? "" : MenuDto.getPath());
        if (!menuService.updateMenu(MenuDto)) {
            return Result.error("修改菜单失败");
        }
        return Result.success("修改菜单成功");
    }

    @GetMapping("/deleteMenu/{id}")
    public Result<Void> deleteMenu(@PathVariable String id) {
        if (!menuService.deleteMenu(id)) {
            return Result.error("删除菜单失败");
        }
        return Result.success("删除菜单成功");
    }
}
